package com.itheima.chapter12.reflect;

import com.itheima.chapter12.reflect.Domain.Student;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
反射类中成员方法
 */
public class Demo4 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Student> studentClass = Student.class;
        System.out.println("------------返回所有公共成员方法对象数组，包括继承----------------");
        Method[] methods = studentClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("------------返回所有成员方法对象数组，不包括继承----------------");
        Method[] declaredMethods = studentClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        //反射指定成员方法对象
        Method eat1 = studentClass.getMethod("eat");
        Method eat2 = studentClass.getMethod("eat", int.class);
        //反射得到构造方法对象，并根据构造方法对象创建Student实例
        Student student = studentClass.getConstructor().newInstance();
        Student student2 = studentClass.getConstructor().newInstance();
        eat2.invoke(student,3);
        eat1.invoke(student2);
    }
}
